﻿<?php

require_once '../include/common.inc.php';
require_once '../include/upfile.class.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

$menu_index = 1;
$page_title = 'JANSEN';
$guide_message = 'Product | Delete Products';
$ppp = 30;
$page_show = '';

//by martin 

function GetProductCateOptions($ary,$l)
{
	$s='';
	$sp = '';
	for($i=0;$i<$l;$i++)
	{
		$sp .= '&nbsp;&nbsp;&nbsp;&nbsp;';	
	}
	foreach($ary as $k => $v)
	{
		$s.= '<option value="'.$k.'">'.$sp.$v["name"].'</option>';	
		if($v["sub_cate"]!=null && count($v["sub_cate"])>0)
		{
			$s.=GetProductCateOptions($v["sub_cate"],$l+1);	
		}
	}
	return $s;
}
$strProductCateOptions = GetProductCateOptions($_categories,0);








getMyParam('sub1_x');
getMyParam('sub2_x');
getMyParam('pid_ary');





if (($sub1_x != "" || $sub2_x != "") && !empty($pid_ary))
 {

  if ($sub1_x != "") 
	  {
        foreach ($pid_ary as $v) {
            $sql_1 = "UPDATE `product` SET `delete_flag` = 0 WHERE `id` = '" . $v . "'";
            $db->query($sql_1);
			
        }
    } elseif ($sub2_x != "")
	   {
        foreach ($pid_ary as $v) {
            $sql_1 = "DELETE FROM `product` WHERE `id` = '" . $v . "'";
            $sql_2 = "DELETE FROM `product_inventory` WHERE `product_id` = '" . $v . "'";
            $sql_3 = "DELETE FROM `product_category` WHERE `product_id` = '" . $v . "'";
			$sql_4 = "DELETE FROM `product_p_setting` WHERE `product_id` = '" . $v . "'";
            $db->query($sql_1) && $db->query($sql_2) && $db->query($sql_3)&& $db->query($sql_4);
		 
        }
    }    
	$urls=$page_url;
    header("LOCATION:".$urls);
}


getMyParam('iid');
getMyParam('cid');
getMyParam('key');
$sql_where = "WHERE 1=1";
$page_url = 'P_deleteProh.php?';

if (is_numeric($cid) || is_numeric($iid)) {
    $page_url = 'P_deleteProh.php?cid=' . $cid . '&iid=' . $iid . '&';

	if ($iid >0 && $cid > 0)
	 {
		$aryIn = get_category(0);
				
		$str_sub_cateid = GetCategoryTreeIDS($aryIn,$cid,false);
		//echo "cids:".$str_sub_cateid;		
		
		$sql = "SELECT i.`product_id` FROM `product_inventory` i , `product_category` c WHERE  i.inventory_id='$iid' AND c.product_id=i.product_id AND category_id IN(".$str_sub_cateid.");";
        $query = $db->query($sql);
        while($rs1 = $db->fetch_array($query)) {
            $tmp_ary[] = $rs1['product_id'];
        }
    } 
	//############################## just inventory############################
	  else if ($iid >0)
	   {
        $sql = "SELECT product_id FROM product_inventory WHERE inventory_id='$iid'";
        $query = $db->query($sql);
        while($rs1 = $db->fetch_array($query))
		{
            $tmp_ary[] = $rs1['product_id'];
        }
    }
	//############################## just category ############################
	 else if ($cid >0) 
	 {
		$aryIn = get_category(0);
		
		$str_sub_cateid = GetCategoryTreeIDS($aryIn,$cid,false);		
		$sql = "SELECT product_id FROM product_category WHERE category_id IN(".$str_sub_cateid.");";
        $query = $db->query($sql);
        while($rs1 = $db->fetch_array($query))
		 {
            $tmp_ary[] = $rs1['product_id'];
        }
    }
   //############################## just check result ############################
    if (!empty($tmp_ary)) 
	{
        $tmp_str = implode(",", $tmp_ary);
        $sql_where .= " AND `id` IN (" . $tmp_str . ")";
    } elseif ($iid >0 || $cid > 0) {
        $sql_where .= " AND `id` =0";
    }
}

if ($key != 'Enter code or name' && trim($key) != '') {
    $page_url .= 'key=' . $key . '&';
    $sql_where .= " AND (`code` LIKE '%$key%' OR `name` LIKE '%$key%')";
}


$sql = "SELECT `id` FROM product ".$sql_where;
$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);
// *** page code start ***
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}
//$page_url = 'p_confirm_delete.php?';
// *** page code end ***

$sql = "SELECT `id`, `name`, `code`, `pic`, `small_pic`, `price_1`, `price_2`, `price_3`, `hidden_flag`, `new_flag`, `comment` FROM product ".$sql_where." ORDER BY `code` DESC LIMIT $start_limit, $ppp";

$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $out_ary[] = $rs;
}

$page_show = getPageStr($sql_all_num, $page, $ppp, $page_url);
$debug = 1;
debuginfo();


$strInventorry="";
$sql = "SELECT `id`, `name` FROM `inventory` ORDER BY `seq` ASC";
$query = $db->query($sql);
$inventory_select_ary[0] = "Select...";
while($rs = $db->fetch_array($query))
{
  $strInventorry.='<option value="'.$rs['id'].'">'.$rs['name'].'</option>';
}


include_once(JANSEN_ROOT . 'admin/menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_P_deleteProh');
require_once PrintEot('a_footer');

?>